package com.reder.practice.common.filter;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

/**
 * Access-Control-Allow-Origin 跨域响应过滤器
 * 由于前端和后端彻底分离，端口号不同，所以存在跨域问题
 *
 * @author Fish
 * ------> 1149062639@qq.com
 * created by 2018/11/12 23:30:59
 */
@WebFilter(urlPatterns = {
        "/*"
})
public class AccessControlAllowOriginFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletResponse response = (HttpServletResponse) resp;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        chain.doFilter(req, resp);
    }
}
